package cn.zcib.film.controller;

import cn.zcib.film.bean.Admin;
import cn.zcib.film.service.AdminService;
import cn.zcib.film.service.SubjectService;
import cn.zcib.film.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;

@Controller
public class IndexController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private SubjectService subjectService;

    @GetMapping(value = {"/","/login"})
    public String loginPage(){
        return "login";
    }

    @PostMapping("/login")
    public String main(String name,String password, HttpSession session, Model model) {
        Admin ad = adminService.login(name);

        if (Objects.equals(null, ad)) {
            model.addAttribute("msg", "用户名不存在");
            return "login";
        }else if (!Objects.equals(ad.getaPassword(), password)){
            model.addAttribute("msg", "账号或密码错误");
            return "login";
        } else{
            //放入session
            session.setAttribute("loginAdmin", ad);
            return "redirect:/main.html";
        }
    }

    /**
     * 去index页面
     * @return
     */
    @GetMapping("/main.html")
    public String indexPage(HttpSession session, Model model){
        //是否登录。 应使用拦截器，过滤器。 这里先做演示
        Object loginUser = session.getAttribute("loginAdmin");
        if (loginUser != null){
            return "main";
        }else {
            //回到登录页
            model.addAttribute("msg","请重新登录");
            return "login";
        }
    }

    /**
     *注销
     * @param session 保存了管理员的登录信息
     * @return 返回登录页面
     * */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();//清空session

        return "redirect:/";
    }
    //去电影管理页面
    @RequestMapping("/toSubjectManager.html")
    public String toSubjectManager(){
        return "tables/subject_table";
    }
    //去类型管理页面
    @RequestMapping("/toTypeManager.html")
    public String toTypeManager(){
        return "tables/type_table";
    }
    //去角色列表页面
    @RequestMapping("/toRoleManager.html")
    public String toRoleManager(){
        return "tables/role_table";
    }
    //去角色分配页面
    @RequestMapping("/toAssignRole.html")
    public String toAssignRole(){
        return "tables/assignRole_table";
    }
    //去用户管理页面
    @RequestMapping("/toUserManager.html")
    public String toUserManager(){
        return "tables/user_table";
    }
    //去主页页面
    @RequestMapping("/toIndexManager.html")
    public String toIndexManager(Model model){
        int countUsers = userService.countUsers();
        model.addAttribute("countUsers", countUsers);
        //统计电影
        int countSubjects = subjectService.countSubjects();
        model.addAttribute("countSubjects", countSubjects);
        return "welcome";
    }
    //去放映管理页面
    @RequestMapping("/toPlayManager.html")
    public String toPlayManager(){
        return "tables/play_table";
    }
    //去菜单管理页面
    @RequestMapping("/toMenuManager.html")
    public String toMenuManager(){
        return "permission/PermissionManager";
    }
    //去到修改密码页面
    @RequestMapping("/toModPwd")
    public String  modPwd(){
        return "../static/page/user-password";
    }
    //去到通知公告页面
    @RequestMapping("/toNoticeManager.html")
    public String  toNoticeManager(){
        return "../static/page/editor";
    }
    //去到数据统计页面
    @RequestMapping("/toDataAnalysis.html")
    public String  toDataAnalysis(){
        return "../static/page/charts";
    }
    //去到日志信息页面
    @RequestMapping("/toLogging.html")
    public String  toLogging(){
        return "log/log";
    }
}
